n = int(input())
if 999 < n < 10000:
    ans = [0]
    k = 2
    num = set() 
    m = str(n)
    num.add(n)
    num.add(int(m[:-1]))
    num.add(int(m[1:]))
    num.add(int(m[0:2]))
    num.add(int(m[1:3]))
    num.add(int(m[2:4]))
    for j in m:
        num.add(int(j))

    for x in num:
        while k < x:
            if x % k == 0:
                break
            k += 1
        else:
            ans.append(x)
        k = 2
    print(max(ans))
else:
    print(0)